package com.addressbook.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.addressbook.utils.SortedListOf;

public class GroupHelper extends WebDriverHelperBase {
	private SortedListOf<GroupData> cachegroups;

	public GroupHelper(ApplicationManager app) {
		super(app);
	}

	public GroupHelper submitNewGroup() {
		click(By.name("submit"));
		cachegroups = null;
		return this;
	}

	public GroupHelper initNewGroup() {
		click(By.name("new"));
		return this;
	}

	public GroupHelper fillGroup(GroupData newGroup) {
		type("group_name", newGroup.getName());
		type("group_header", newGroup.getHeader());
		type("group_footer", newGroup.getFooter());
		return this;
	}

	public GroupHelper selectGroupByIndex(int index) {
		click(By.xpath("//input[@name='selected[]'][" + (index+1) + "]"));
		return this;
	}
	
	public GroupHelper submitDeleteGroup() {
		click(By.name("delete"));
		cachegroups = null;
		return this;
	}

	public GroupHelper submitEditGroup() {
		click(By.name("edit"));
		return this;
	}

	public GroupHelper submitUpdate() {
		click(By.name("update"));
		cachegroups = null;
		return this;
	}
	
	public GroupHelper returnToGroupList() {
		click(By.linkText("group page"));
		return this;
	}

	public SortedListOf<GroupData> getGroupsName() {
		if (cachegroups == null){
			rebuildCache();
		}		
		return cachegroups;
	}
	
	private void rebuildCache() {
		cachegroups = new SortedListOf<GroupData>();
		manager.navigateTo().GroupList();
		List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@name='selected[]']"));
		for (WebElement checkbox : checkboxes) {
			GroupData group = new GroupData();
			String name = checkbox.getAttribute("title");
			group.setName(name.substring("Select (".length(), name.length() - ")".length()));
			cachegroups.add(group);
		}		
	}

	public GroupData getGroupByIndex(int index) {		
		selectGroupByIndex(index).submitEditGroup();			
		GroupData group = new GroupData()
			.withName(getValueByName("group_name"))
			.withHeader(getTextByName("group_header"))
			.withFooter(getTextByName("group_footer"));
		manager.navigateTo().GroupList();
		return group;
	}
	
	public SortedListOf<GroupData> getFullGroups() {
		cachegroups = new SortedListOf<GroupData>();
		manager.navigateTo().GroupList();
		for (int i = 0; i < driver.findElements(By.xpath("//input[@name='selected[]']")).size(); i++) {
			GroupData group = getGroupByIndex(i);
			cachegroups.add(group);
		}
		return cachegroups;
	}

	public GroupHelper createGroup(GroupData group) {
		manager.navigateTo().GroupList();
	    initNewGroup();
		fillGroup(group);
		submitNewGroup();
		returnToGroupList();
		rebuildCache();
		return this;
	}

	public GroupHelper editGroup(int index, GroupData group) {
		manager.navigateTo().GroupList();
		selectGroupByIndex(index);
		submitEditGroup();
		fillGroup(group);
		submitUpdate();
		returnToGroupList();
		rebuildCache();
		return this;
	}
	
	public GroupHelper deleteGroup(List<Integer> index) {
		manager.navigateTo().GroupList();
		for (int i = 0; i < index.size(); i++) {
			selectGroupByIndex(index.get(i));
		}		
		submitDeleteGroup();
		returnToGroupList();
		rebuildCache();
		return this;
	}

	public int getGroupListSize() {
		getGroupsName();
		return cachegroups.size();
	}
}
